package com.heima.big_event03.service.impl;

import com.heima.big_event03.entity.Category;
import com.heima.big_event03.entity.User;
import com.heima.big_event03.mapper.CategoryDao;
import com.heima.big_event03.service.CategoryService;
import com.heima.big_event03.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageImpl;
//import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2025-03-04 20:44:22
 */
@Service("categoryService")
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Category queryById(String id) {
        return this.categoryDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param category 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
//    @Override
//    public Page<Category> queryByPage(Category category, PageRequest pageRequest) {
//        long total = this.categoryDao.count(category);
//        return new PageImpl<>(this.categoryDao.queryAllByLimit(category, pageRequest), pageRequest, total);
//    }

    /**
     * 新增数据
     *
     * @param category 实例对象
     * @return 实例对象
     */
    @Override
    public Category insert(Category category) {
        this.categoryDao.insert(category);
        return category;
    }

    /**
     * 修改数据
     *
     * @param c 实例对象
     * @return 实例对象
     */
    @Override
    public void update(Category c) {
        c.setUpdateTime(LocalDateTime.now());
        this.categoryDao.update(c);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.categoryDao.deleteById(id) > 0;
    }

    /**
     * 新增文章分类
     * @return
     */
    @Override
    public void add(Category category) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String id = (String) claims.get("id");
        log.info("id:"+id);
        category.setCreateUser(id);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        this.categoryDao.insert(category);
    }

    /**
     * 文章分类列表
     *
     * @return
     */
    @Override
    public List<Category> list() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String id = (String) claims.get("id");
        List<Category> list = this.categoryDao.list(id);
        return list;
    }
}
